package com.mingmen.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.model.AuditListModel;

import java.util.ArrayList;
import java.util.List;


public class AuditListAdapter extends RecyclerView.Adapter<AuditListAdapter.ViewHolder> {
    List<AuditListModel.DataBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<AuditListModel.DataBean> myLiveList);
    }


    public AuditListAdapter(Context context, List<AuditListModel.DataBean> myLiveList) {
        this.mContext = context;
        this.myLiveList = myLiveList;

    }

    public void setDataList(List<AuditListModel.DataBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_auditlist, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        AuditListModel.DataBean dataBean = myLiveList.get(position);
        holder.tv_label.setText(myLiveList.get(position).getCategoryName());
        holder.commodityName.setText(dataBean.getCommodityName());
        holder.spec.setText("规格: " + dataBean.getSpec());
        holder.description.setText(dataBean.getDescription());
        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);

        if (dataBean.getState().equals("2")) {
            holder.text_manager.setVisibility(View.VISIBLE);
            holder.text_state.setBackground(mContext.getResources().getDrawable(R.drawable.shape_blue_normal_bg));
            holder.text_state.setText("已通过");

        } else if (dataBean.getState().equals("1")) {
//            text_manager
            holder.text_manager.setVisibility(View.VISIBLE);
            holder.text_manager.setBackground(mContext.getResources().getDrawable(R.color.white));
            holder.text_manager.setTextColor(Color.parseColor("#B7150D"));
            holder.text_manager.setText("商品已存在");
            holder.text_state.setText("已拒绝");
            holder.text_state.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.text_state.setBackground(mContext.getResources().getDrawable(R.drawable.shape_rednormal_bg));
        } else if (dataBean.getState().equals("4")) {
            holder.text_state.setBackground(mContext.getResources().getDrawable(R.drawable.shape_yellow));
            holder.text_state.setText("审核中");
            holder.text_manager.setVisibility(View.GONE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.text_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_label;
        LinearLayout content;
        TextView text_manager;
        TextView commodityName;
        TextView spec;
        TextView description;
        TextView text_state;

        ImageView ivAvatar;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
            content = itemView.findViewById(R.id.content);
            text_manager = itemView.findViewById(R.id.text_manager);
            commodityName = itemView.findViewById(R.id.commodityName);
            spec = itemView.findViewById(R.id.spec);
            description = itemView.findViewById(R.id.description);
            text_state = itemView.findViewById(R.id.text_state);
            ivAvatar= itemView.findViewById(R.id.ivAvatar);
        }
    }


}
